/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uet.questiondetector.factory;

import edu.uet.questiondetector.patterns.LHS;
import edu.uet.questiondetector.patterns.LSP;
import edu.uet.questiondetector.utils.TaggerTool;
import edu.uet.questiondetector.utils.WordToken;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author thinhnk
 */
public class PatternFactory {
    public static LHS parseLHS(List<WordToken> words){
        LHS lhs = new LHS();
        for(WordToken w: words){
            if(QuestionKeyword.instances().isKeyword(w.getContent())){
                lhs.add(w.getContent());
            }else{
                lhs.add(w.getTag());
            }
        }
        return lhs;        
    }
    public static void createPatterns(String inFile, String outFile, String label){
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.newDocument();
            //Khoi tao root
            Element root = doc.createElement("root");
            doc.appendChild(root);
            
            Scanner sc = new Scanner(new File(inFile));
            String sen = null;
            LHS lhs = null;
            Element pattern = null;
            Element sentence = null;
            Element lhsElement = null;
            Element labelElement = null;
            while(sc.hasNext()){
                //Khoi tao pattern
                pattern = doc.createElement("pattern");
                root.appendChild(pattern);
                
                sen = sc.nextLine();
                if(!sen.endsWith("X-X-X")){
                    //sentence element
                    sentence = doc.createElement("sentence");
                    sentence.appendChild(doc.createTextNode(sen));
                    pattern.appendChild(sentence);
                    
                    lhs = createLHS(sen);
                    // lhs element
                    lhsElement = doc.createElement("lhs");
                    lhsElement.appendChild(doc.createTextNode(lhs.toString()));
                    pattern.appendChild(lhsElement);
                    //label element
                    labelElement = doc.createElement("label");
                    labelElement.appendChild(doc.createTextNode(label));
                    pattern.appendChild(labelElement);
                }
            }
            Source xmlSource = new DOMSource(doc);
            Result result = new StreamResult(new FileOutputStream(outFile));
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty("indent", "yes");
            transformer.transform(xmlSource, result);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PatternFactory.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(PatternFactory.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(PatternFactory.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(PatternFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public static LHS createLHS(String text){
        return parseLHS(TaggerTool.instance().tagToList(text));
    }
}
